// Implementation of the ClientSocket class

#include "ClientSocket.h"
#include "SocketException.h"
#include <fstream>
#include <iostream>
#include <stdlib.h>

using namespace std;


ClientSocket::ClientSocket ()
{
}

ClientSocket::ClientSocket ( string host, int port )
{
  
  if ( ! Socket::create() )
    {
      throw SocketException ( "Could not create client socket." );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( "Could not bind to port." );
    }

}

void ClientSocket::init (string host, int port )
{
  if ( ! Socket::create( ) )
    {
      throw SocketException ( "Could not create client socket." );
    }

  if ( ! Socket::connect ( host, port ) )
    {
      throw SocketException ( "Could not bind to port." );
    }

}

const ClientSocket& ClientSocket::operator << ( const string& s ) const
{
  if ( ! Socket::send ( s ) )
    {
      throw SocketException ( "Could not write to socket." );
    }

  return *this;

}

const ClientSocket& ClientSocket::operator >> ( string& s ) const
{
  int ret=Socket::recv( s );
  if ( ret==0 )
    {
      throw SocketException ( "Could not read from socket." );
    }

  return *this;
}


const ClientSocket& ClientSocket::sendfile(string& fname) const
{
  Socket::sendfile(fname);
  return *this;
}

string ClientSocket::recvfile() const
{
  return Socket::recvfile();
}

void ClientSocket::close(){
  Socket::close();
}

void ClientSocket::set_non_blocking(bool b){
  Socket::set_non_blocking(b);
}
